/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// fmod_sound.cpp - 

#include "fmod_local.h"

/*-----------------------------------------------------------------------------
	EFMODSound3D
-----------------------------------------------------------------------------*/

//
//	EFMODSound3D::EFMODSound3D
//
EFMODSound3D::EFMODSound3D ( EFMODSoundSystem *ss_system, EFMODSoundBuffer *buffer )
{
	this->ss_system = ss_system;
	this->buffer	= buffer;
	fmod_channel	= NULL;
	
	SetPose				( EPoint(), EQuaternion() );
	SetVelocity			( EVector() );
	SetVolume			( 1 );
	SetPitch			( 1 );
	SetMute				( false );		
	SetSpread			( ss_system->sound_spread->Float() );
	SetDoplerLevel		( ss_system->sound_dopler_level->Float() );
	SetMinMaxDistance	( ss_system->sound_min_distance->Float(), ss_system->sound_max_distance->Float() );
}


//
//	EFMODSound3D::~EFMODSound3D
//
EFMODSound3D::~EFMODSound3D ( void )
{
	Stop();
}


//
//	EFMODSound3D::Play
//
void EFMODSound3D::Play ( bool loop )
{
	Stop();

	fmod_channel = ss_system->CreateChannel(buffer->GetFMODSound());
		
	//	actuate values (apply stored values to fmod_channel) :
	SetPose				( position, orient );
	SetVelocity			( velocity );
	SetVolume			( volume );
	SetPitch			( pitch );
	SetMute				( mute );		
	SetSpread			( spread );
	SetDoplerLevel		( dopler_level );
	SetMinMaxDistance	( min_distance, max_distance );

	if (fmod_channel) {
		if (loop) {
			FMOD_CHECK( fmod_channel->setMode(FMOD_LOOP_NORMAL) ); 
		}
		else {
			FMOD_CHECK( fmod_channel->setMode(FMOD_LOOP_OFF) ); 
		}

		FMOD_CHECK( fmod_channel->setPaused(false) );
	}
}


//
//	EFMODSound3D::Stop
//
void EFMODSound3D::Stop ( void )
{
	if (fmod_channel) {
		FMOD_CHECK( fmod_channel->stop() );
		fmod_channel = NULL;
	}
}


bool EFMODSound3D::CheckChannel( void ) const
{
	if (!fmod_channel) {
		return false;
	}
	
	float dummy;
	
	//	do anything with a channel :
	FMOD_RESULT	result = fmod_channel->getVolume(&dummy);
	
	if ( result == FMOD_ERR_INVALID_HANDLE ) {
		//	channel is freed :
		fmod_channel = NULL;
		return false;		
	}
	
	return true;
}


//
//	EFMODSound3D::Pause
//
void EFMODSound3D::Pause ( void )
{
	if (CheckChannel()) {
		FMOD_CHECK( fmod_channel->setPaused(true) );
	}
}


//
//	EFMODSound3D::Unpause
//
void EFMODSound3D::Unpause ( void )
{
	if (CheckChannel()) {
		FMOD_CHECK( fmod_channel->setPaused(false) );
	}
}


//
//	EFMODSound3D::IsPlaying
//
bool EFMODSound3D::IsPlaying(void) const
{
	bool is_playing = false;

	if (CheckChannel()) {
		FMOD_CHECK( fmod_channel->isPlaying(&is_playing) );
	}

	return is_playing;
}


//
//	EFMODSound3D::SetPose
//
void EFMODSound3D::SetPose( const EPoint &position, const EQuaternion &orient )
{
	this->position	=	position;
	this->orient	=	orient;
	
	if (CheckChannel()) {
		FMOD_VECTOR pos_vector	= { position.x, position.y, position.z };
		FMOD_CHECK( fmod_channel->set3DAttributes( &pos_vector, NULL ) );
	}
}


//
//	EFMODSound3D::GetPose
//
void EFMODSound3D::GetPose( EPoint &position, EQuaternion &orient ) const
{
	position	=	this->position;
	orient		=	this->orient;
}


//
//	EFMODSound3D::SetVelocity
//
void EFMODSound3D::SetVelocity( const EVector &vel )
{
	this->velocity	=	vel;

	if (CheckChannel()) {
		FMOD_VECTOR vel_vector	= { vel.x, vel.y, vel.z };
		FMOD_CHECK( fmod_channel->set3DAttributes( NULL, &vel_vector ) );
	}
}


//
//	EFMODSound3D::GetVelocity
//
void EFMODSound3D::GetVelocity( EVector &linear_velocity ) const
{
	linear_velocity	=	this->velocity;
}


//
//	EFMODSound3D::SetVolume
//
void EFMODSound3D::SetVolume ( float volume )
{
	this->volume = volume;
	
	if (CheckChannel()) {
		FMOD_CHECK( fmod_channel->setVolume( volume ) );
	}
}


//
//	EFMODSound3D::SetPitch
//
void EFMODSound3D::SetPitch ( float pitch )
{
	this->pitch = pitch;
	
	if (CheckChannel()) {
		float frequency = 0;
		FMOD_CHECK( fmod_channel->getFrequency( &frequency ) );

		frequency *= pitch;
		FMOD_CHECK( fmod_channel->setFrequency( frequency ) );
	}
}


//
//	EFMODSound3D::SetMute
//
void EFMODSound3D::SetMute ( bool mute )
{
	this->mute	=	mute;
	
	if (CheckChannel()) {
		FMOD_CHECK( fmod_channel->setMute( mute ) );
	}
}


//
//	EFMODSound3D::SetSpread
//
void EFMODSound3D::SetSpread ( float angle )
{
	spread = angle;
	 
	if (CheckChannel()) {
		FMOD_CHECK( fmod_channel->set3DSpread( angle ) );
	}
}


//
//	EFMODSound3D::SetDoplerLevel
//
void EFMODSound3D::SetDoplerLevel (  float level )
{
	dopler_level = level;
	
	if (CheckChannel()) {
		FMOD_CHECK( fmod_channel->set3DDopplerLevel( level ) );
	}
}


//
//	EFMODSound3D::SetMinMaxDistance
//
void EFMODSound3D::SetMinMaxDistance ( float min, float max )
{
	min_distance = min;
	max_distance = max;
	
	if (CheckChannel()) {
		FMOD_CHECK( fmod_channel->set3DMinMaxDistance( min, max ) );
	}
}

